package com.gxh.controller.user;

import com.gxh.dto.FavoriteDTO;
import com.gxh.result.Result;
import com.gxh.service.FavoriteService;
import com.gxh.vo.MachineItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import java.util.List;


@RestController("UserFavoriteController")
@RequestMapping("/gxh/user/favorite")
@Api(tags = "用户收藏接口")
@Slf4j
public class FavoriteController {


    @Autowired
    private FavoriteService favoriteService;

    /**
     * 用户收藏
     * @param favoriteDTO
     * @return
     */
    @PostMapping
    @ApiOperation("用户收藏")
    public Result save(@RequestBody FavoriteDTO favoriteDTO){
        log.info("用户收藏{}" ,favoriteDTO);
        favoriteService.save(favoriteDTO);
        return Result.success();
    }

    /**
     * 用户收藏列表
     * @return machineItemVOS
     */
    @GetMapping("/list")
    @ApiOperation("用户收藏列表")
    public Result<List<MachineItemVO>> list(){
        log.info("用户收藏列表");
        List<MachineItemVO> machineItemVOS = favoriteService.list();
        return Result.success(machineItemVOS);
    }

    /**
     * 用户收藏数量
     * @return
     */
    @GetMapping("/count")
    @ApiOperation("用户收藏数量")
    @Cacheable("favoriteCount")
    public Result<Integer> count(){
        log.info("用户收藏数量");
        Integer count = favoriteService.count();
        return Result.success(count);
    }

    /**
     * 用户取消收藏
     * @param id
     * @return null
     */
    @DeleteMapping("/{id}")
    @ApiOperation("用户取消收藏")
    @CacheEvict(value = "favoriteCount",allEntries = true)
    public Result delete(@PathVariable Long id){
        log.info("用户取消收藏{}" ,id);
        favoriteService.delete(id);
        return Result.success();
    }

}
